package com.mzj.saas.mservice.house.repository;

import com.mzj.saas.mservice.house.entity.House;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface HouseRepository extends PagingAndSortingRepository<House, String>, JpaSpecificationExecutor<House> {

    List<House> findByIdIn(List<String> houseIds);

    List<House> findByResidenceCommunityIdAndBuildingNumAndUnit(String residenceCommunityId, Long buildingNum, Long unit);

    List<House> findByResidenceCommunityIdAndBuildingNumAndUnitAndRoomNumber(String residenceCommunityId, Long buildingNum, Long unit,String roomNumber);

    List<House> findByResidenceCommunityIdAndBuildingNumAndUnitOrderByRoomNumber(String residenceCommunityId, Long buildingNum, Long unit);

    @Override
    void deleteById(String s);

    @Modifying
    @Query(value = "UPDATE saas_house h SET h.match_status = ?4 WHERE h.residence_community_id = ?1 AND h.org_id = ?2 AND h.match_status = ?3 AND h.audit_status = 1 and h.house_nature = 1", nativeQuery = true)
    int updateMatchStatus(String residenceCommunityId, Long orgId, Integer matchStatus, Integer status);

    List<House> findByOrgIdIn(List<Long> orgIds);

    List<House> findByOrgId(Long orgId);
    List<House> findByOrgIdAndStatus(Long orgId,Integer status);
    List<House> findByUnitLesseeIdAndStatus(Long orgId,Integer status);

    @Transactional
    @Modifying
    @Query(value = "UPDATE saas_house h SET h.pwd_lock_status = ?2 WHERE h.id = ?1", nativeQuery = true)
    int updateLockStatusById(String houseId, Integer status);

    int countByResidenceCommunityId(String residenceCommunityId);

    List<House> findByResidenceCommunityId(String residenceCommunityId);

    List<House> findByUnitAndRoomNumber(Long unit, String roomNumber);

    List<House> findByResidenceCommunityIdAndBuildingNumAndUnitAndHouseNatureIn(String residenceCommunityId, Long buildingNum, Long unit, List<Integer> houseNature);

    List<House> findByWaterMeter(String waterMeter);

    List<House> findByElectricMeter(String electricMeter);

    List<House> findByGasMeter(String gasMeter);

    List<House> findByHousePropertyIdIn(List<Long> housePropertyId);

    List<House> findByResidenceCommunityIdIn(List<String> residenceCommunityId);

    List<House> findByResidenceCommunityIdAndBuildingNum(String residenceCommunityId,Long buildingNum);

    List<House> findByHouseNature(Integer houseNature);

    List<House> findByHouseNatureAndIdIn(Integer houseNature, List<String> hList);

    @Modifying
    @Query(value = "UPDATE saas_house SET match_status = 0,lease_status = 0,payment_status=0,record_status=0,pwd_lock_status=0,check_satus=0,record_change_status = NULL,renewal_status = NULL,match_type = NULL,apply_code=NULL,contract_number=NULL,start_time = NULL WHERE id = ?1",nativeQuery = true)
    Integer updateWithDraw(String houseId);

    List<House> findByHousemateIdAndStatus(String housemateId,Integer status);
    List<House> findByIdAndStatus(String housemateId,Integer status);

    List<House> findByHousemateIdAndStatusAndRoomNumber(String housemateId,Integer status,String roomNumber);

    House findByUnitAndRoomNumberAndStatus(Long unit, String roomNumber, int status);

    House findByWaterMeterAndStatus(String waterMeter, int status);

    House findByElectricMeterAndStatus(String electricMeter, int status);

    House findByGasMeterAndStatus(String gasMeter, int status);
}
